package io.zsy.producer;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息生产者工具类，通过调用该类的send方法向MQ生产消息
 * <p>
 * EnableBinding中的value值Source，作用是将消息通道与配置文件中配置的交换机进行绑定
 *
 * @author: zhangshuaiyin
 * @date: 2021/12/22 20:36
 */
@Component
@EnableBinding(Source.class)
public class MessageProducer {
    @Resource
    Source source;

    /**
     * 发送消息
     *
     * @param message message
     */
    public void send(String message) {
        source.output().send(MessageBuilder.withPayload(message).build());
    }

}
